<?php

require_once 'Zend/Application/Resource/ResourceAbstract.php';
require_once 'Core/Resource.php';

/**
 * This application resource allows you to configure various html header elements used by our application,
 * including scripts, styles and links.
 * 
 * @usage
 * resources.html.head.link.ico.href=/images/favicon.ico
 * resources.html.head.link.ico.rel=favicon
 * 
 * resources.html.head.script.jquery=https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js
 * resources.html.head.script.jquery_ui=https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js
 * 
 * resources.html.head.meta.fb_app_id.content=asfdfasdfsafdsfdsa
 *
 * @author Michael Krotscheck
 *
 */
class Core_Application_Resource_Html extends Zend_Application_Resource_ResourceAbstract
{

	/**
	 * Defined by Zend_Application_Resource_Resource
	 *
	 * @return Zend_Navigation
	 */
	public function init ()
	{
		$options = $this->getOptions();
		
		if (! empty($options['head']))
		{
			$head_options = $options['head'];
			
			if (isset($head_options['link']))
			{
				$this->addLinks($head_options['link']);
			}
			
			if (isset($head_options['script']))
			{
				$this->addScripts($head_options['script']);
			}
			
			if (isset($head_options['meta']))
			{
				$this->addMetas($head_options['meta']);
			}
		}
		
		return null;
	}

	/**
	 * Andd <link> elements.
	 * 
	 * @param array $links
	 */
	private function addLinks ($links)
	{
		// Get the view.
		$view = $this->getView();
		
		foreach ( $links as $link )
		{
			$view->headLink()->headLink($link);
		}
	}

	/**
	 * Add <script> elements
	 * 
	 * @param array $scripts
	 */
	private function addScripts ($scripts)
	{
		// Get the view.
		$view = $this->getView();
		
		foreach ( $scripts as $script )
		{
			$view->headScript()->appendFile($script);
		}
	}

	/**
	 * Add meta elements.
	 * 
	 * @param array $metas
	 */
	private function addMetas ($metas)
	{
		// Get the view.
		$view = $this->getView();
		
		foreach ( $metas as $meta )
		{
			$view->headMeta()->headMeta($meta['content'], $meta['value'], $meta['type']);
		}
	}

	/**
	 * Get an instance of our active view resource.
	 */
	public function getView ()
	{
		$bootstrap = $this->getBootstrap();
		if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper && $bootstrap->hasPluginResource('view'))
		{
			$view = $bootstrap->bootstrap('view')->getResource('view');
			return $view;
		}
	}

}
